<template>
    <div>
        <div><h1>商家列表</h1></div>
        <div>
            <el-table :data="userList" stripe style="width: 100%">
                <el-table-column prop="userId" label="用户ID" width="180" />
                <el-table-column prop="userName" label="登录账号" width="180" />
                <el-table-column label="操作" />
            </el-table>
        </div>
        <div style="margin-top: 20px">
            <el-pagination
                v-model:current-page="searchForm.pageNum"
                v-model:page-size="searchForm.pageSize"
                :page-sizes="[2, 4, 6, 8]"
                :background="true"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
            />
        </div>
    </div>
</template>

<script setup lang="ts">
import { getUserList } from "@/api/api";
import { onMounted, ref } from "vue";

const searchForm = ref({
    pageNum: 1,
    pageSize: 2,
});
const total = ref(0);
const userList = ref([]);

const handleSizeChange = (val: number) => {
    console.log(`${val} items per page`);
    searchForm.value.pageSize = val;
    loadUserList();
};
const handleCurrentChange = (val: number) => {
    console.log(`current page: ${val}`);
    searchForm.value.pageNum = val;
    loadUserList();
};

// 获取用户列表
const loadUserList = () => {
    getUserList(searchForm.value).then((res) => {
        if (res.data.code == 200) {
            total.value = res.data.data.total;
            userList.value = res.data.data.records;
        }
    });
};

// 页面加载时加载用户列表
onMounted(() => {
    loadUserList();
});
</script>

<style scoped></style>
